#!/bin/bash
# File checked with ShellCheck (https://www.shellcheck.net/)

# NOTE: This file must have the same contents than
# kms-core/debian/kms-core.postinst



# Bash options for strict error checking
set -o errexit -o errtrace -o pipefail -o nounset

# Trace all commands
set -o xtrace



# Settings
HOME_DIR="/var/lib/kurento"



# Create the kurento user
if ! id kurento >/dev/null 2>&1; then
    adduser \
        --system --group \
        --home "$HOME_DIR" \
        --disabled-password \
        kurento
fi



# Prepare the kurento home directory
mkdir -p "$HOME_DIR"
chown -R kurento "$HOME_DIR"
chmod 777 "$HOME_DIR"

# FIXME+DEPRECATED: Old home dir was "/var/kurento"
# keep it as a symlink to avoid breaking user's setups,
# but delete this code once releasing next major version, Kurento 7.0
HOME_DIR_OLD="/var/kurento"
if [[ -d "$HOME_DIR_OLD" && ! -L "$HOME_DIR_OLD" ]]; then
    mv "$HOME_DIR_OLD"/* "$HOME_DIR_OLD"/.* "$HOME_DIR"/ 2>/dev/null || true
    rmdir "$HOME_DIR_OLD"
    ln -s -T "$HOME_DIR" "$HOME_DIR_OLD"

    # Set the new dir as user's dir in /etc/passwd
    usermod --home "$HOME_DIR" kurento
fi

#DEBHELPER#
exit 0
